package cn.leizp.springboot.server.netmanger.telnet;

import cn.leizp.springboot.server.netmanger.common.inter.IMangerFunc;
import cn.leizp.springboot.server.netmanger.common.inter.IpMacTable;
import cn.leizp.springboot.server.netmanger.common.inter.ResultData;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service("TelnetImpl") // 使用 @Service 注解注册为 Spring Bean，并指定 Bean 名称为 SnmpImpl
public class TelnetFunc implements IMangerFunc {
    public String getConfig() {
        return "telnet Server";
    }

    public ResultData sendCommand(String switchIp, String command){
        ResultData resultData = null;
        MySwitch mySwitch = MySwitch.getInstance(switchIp);
        return mySwitch.processCommand(command);
    }

    public ResultData getCurUserBind(String mySwitchIp){
        ResultData resultData = sendCommand(mySwitchIp, "display current-configuration");
        if(resultData.code == 1){
            String[] msgArray = resultData.message.split("\r\n");
            List<IpMacTable> bindDataList = new ArrayList<>();
            // 定义匹配模式
            Pattern pattern = Pattern.compile(
                    "user-bind static ip-address (\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})" +
                            " mac-address ([\\w-]+)" +
                            "(?: interface (\\S+))?" +  // 可选接口
                            "(?: vlan (\\d+))?"        // 可选VLAN
            );
            for (int i = 0; i < msgArray.length; i++) {
                if(msgArray[i].startsWith("user-bind static")){
                    try{
                        Matcher matcher = pattern.matcher(msgArray[i]);
                        if (matcher.find()) {
                            bindDataList.add(new IpMacTable(matcher.group(1), matcher.group(2), matcher.group(3), matcher.group(4)));
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            resultData.data = bindDataList;
        }
        return resultData;
    }

    public ResultData getCurArpTable(String mySwitchIp){
        ResultData resultData = sendCommand(mySwitchIp, "display arp");
        if(resultData.code == 1){
            String[] msgArray = resultData.message.split("\r\n");
            List<IpMacTable> bindDataList = new ArrayList<>();
            // 定义匹配模式
            Pattern pattern = Pattern.compile(
                    "\\b(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})\\b" +  // IP地址
                            ".*?\\b([\\da-fA-F]{4}-[\\da-fA-F]{4}-[\\da-fA-F]{4})\\b" +  // MAC地址
                            "(?:.*?\\b(\\d+)\\b)?" +  // 可选数字（如20）
                            "(?:.*?\\b([A-Z]-\\d+)\\b)?" +  // 可选字母数字组合（如D-0）
                            "(?:.*?\\b([A-Za-z]+\\d+/\\d+/\\d+)\\b)?"  // 可选接口（如GE0/0/1）
            );
            for (int i = 0; i < msgArray.length; i++) {
                try{
                    Matcher matcher = pattern.matcher(msgArray[i]);
                    if (matcher.find()) {
                        bindDataList.add(new IpMacTable(matcher.group(1), matcher.group(2), matcher.group(5), null));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            resultData.data = bindDataList;
        }
        return resultData;
    }




    public static void main(String[] args) {
        System.out.println("start main");

        TelnetFunc telnetFunc = new TelnetFunc();

        ResultData resultData = telnetFunc.getCurArpTable("192.168.55.1");
        if(resultData.code == 1){
            for (IpMacTable bd : (List<IpMacTable>)resultData.data) {
                System.out.println("IP地址: " + bd.ipAddr + "， MAC地址：" + bd.macAddr + "，接口：" + bd.interNum);
            }
        }

            System.out.println("配置完成");
    }
}
